package cn.practice.chat.base;

import cn.practice.chat.response.user.LoginUser;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.servlet.http.HttpServletRequest;

public class BaseController {

    @Autowired
    HttpServletRequest req;

    /**
     * @description 获取当前的登录用户
     * @return {@link LoginUser}
     **/
    public LoginUser getUser(){
        UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        LoginUser userDetails = (LoginUser) authenticationToken.getPrincipal();
        System.out.println("当前登录：" + JSONObject.toJSONString(userDetails));
        return userDetails;
    }
}
